package com.privacy.accountmgr.views.numberpasswordview

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.AdapterView
import android.widget.GridView
import android.widget.LinearLayout
import android.widget.TextView
import com.privacy.accountmgr.R

import java.util.Stack

class NumberPasswordView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr) {
    private var passwordStack: Stack<String>? = null

    private var gvKeyboard: GridView? = null

    private var nvPassword: NumberView? = null

    private var tvTip: TextView? = null

    private var onInputNumberCodeCallback: OnInputNumberCodeCallback? = null

    private val password: String
        get() {
            val password = StringBuilder()
            for (number in passwordStack!!) {
                password.append(number)
            }
            return password.toString()
        }

    init {
        init()
    }

    private fun init() {
        passwordStack = Stack()

        val view = View.inflate(context, R.layout.view_number_password, this)
        gvKeyboard = view.findViewById<View>(R.id.gv_keyboard) as GridView
        nvPassword = view.findViewById<View>(R.id.nv_password) as NumberView
        tvTip = view.findViewById<View>(R.id.tv_tip) as TextView

        val adapter = NumberKeyboardAdapter(context)

        gvKeyboard!!.adapter = adapter

        gvKeyboard!!.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            if (position == NUMBER_BUTTON_CLEAR) {

                // 清空
                //                    if(!passwordStack.empty()) {
                //                        passwordStack.clear();
                //                        // 刷新界面
                //                        nvPassword.setEnteredCount(0);
                //                    }
                return@OnItemClickListener
            } else if (position == NUMBER_BUTTON_DELETE) {
                if (passwordStack!!.empty() || passwordStack!!.size > NUMBER_COUNT) {
                    return@OnItemClickListener
                }
                // 删除
                passwordStack!!.pop()
                // 刷新界面
                nvPassword!!.setEnteredCount(passwordStack!!.size)
                return@OnItemClickListener
            } else {
                if (passwordStack!!.size < 4) {
                    passwordStack!!.push(adapter.getItem(position))
                    // 刷新界面
                    nvPassword!!.setEnteredCount(passwordStack!!.size)
                }
            }

            if (passwordStack!!.size == NUMBER_COUNT) {
                // 输入满4位密码，回调
                if (onInputNumberCodeCallback != null) {
                    onInputNumberCodeCallback!!.onResult(password)
                }
            }
        }
    }

    /**
     * 设置回调监听
     * @param onInputNumberCodeCallback
     */
    fun setOnInputNumberCodeCallback(onInputNumberCodeCallback: OnInputNumberCodeCallback) {
        this.onInputNumberCodeCallback = onInputNumberCodeCallback
    }

    /**
     * 设置文字tip
     * @param tip
     */
    fun setTip(tip: String) {
        tvTip!!.text = tip
    }

    /**
     * 清空输入框
     */
    fun clearNumber() {
        nvPassword!!.setEnteredCount(0)
        passwordStack!!.clear()
    }

    interface OnInputNumberCodeCallback {
        fun onResult(code: String)
    }

    companion object {
        private val NUMBER_BUTTON_DELETE = 11 // 删除按键
        private val NUMBER_BUTTON_CLEAR = 9   // 清空按键
        private val NUMBER_COUNT = 4          // 4位密码
    }
}
